<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <% String path = request.getContextPath();%>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="<%=path%>/layui/css/layui.css">
    <script src="<%=path%>/layui/layui.js" type="text/javascript"></script>
    <script src="<%=path%>/jquery-3.4.1.js" type="text/javascript"></script>
</head>
<body onload="init();">
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-form" style="margin-top: 8%;">
            <div class="layui-form-item">
                <label class="layui-form-label">实验室名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="input_labName" name="labName" required lay-verify="required" placeholder="实验室名称" class="layui-input" disabled>
                </div>
                <div style="color: red;font-size: larger;">*</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">实验室容量</label>
                <div class="layui-input-inline">
                    <input type="text" id="input_labCapa" name="labCapa" required lay-verify="required" oninput="if(value<0)value=0;value=value.replace(/[^\d]/g,'')" placeholder="实验室容量" class="layui-input">
                </div>
                <div style="color: red; font-size: larger;">*</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">实验室类型</label>
                <div class="layui-input-inline">
                    <select id="select_labType" name="labType" required lay-verify="required">
                        <option value=""></option>
                        <option value="0">普通</option>
                        <option value="1">专业</option>
                    </select>
                </div>
                <div style="color: red; font-size: larger;">*</div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">实验室介绍</label>
                <div class="layui-input-block">
                      <textarea id="input_labRes" name="labRes" placeholder="请输入申请理由(250字以内)：" class="layui-textarea" maxlength="250"
                                style="resize: none;"></textarea>
                </div>
            </div>
            <div class="layui-form-item" style="margin-top: 8%;  margin-left: 13%;">
                <div class="layui-input-block">
                    <button id="btn" class="layui-btn layui-btn-normal" onclick="modify();">确认修改</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function init() {
        parent.buttonID=parent.buttonID.split("_")[1];
        layui.use('form',function () {
            let form = layui.form;
            $(function () {
                $('#input_labName').val($(window.parent.document).find("#title_"+parent.buttonID).text().trim());
                $('#input_labRes').val($(window.parent.document).find("#detial_"+parent.buttonID).text().trim());
                $.ajax({
                    type:"POST"
                    ,dataType:"json"
                    ,url:"${pageContext.request.contextPath}/jsp/root.do?method=getLabDataByLabName&labName="+$('#input_labName').val()
                    ,success:function (data) {
                        $('#input_labCapa').val(data.labData.labTotal);
                        let select = $('#select_labType').find('option');
                        let str="<option value=''></option>";
                        for (let i = 0; i < select.length; i++) {
                            if ((select[i].value)==data.labData.labTag){
                                if (data.labData.labTag == 1){
                                    str+="<option selected value=\""+1+"\">"+'专业'+"</option>"+"<option value=\""+0+"\">"+'普通'+"</option>";
                                }else if (data.labData.labTag == 0){
                                    str+="<option selected value=\""+0+"\">"+'普通'+"</option>"+"<option value=\""+1+"\">"+'专业'+"</option>";
                                }
                                $('#select_labType').html(str);
                                form.render("select");
                                break;
                            }
                        }
                    }
                })
            })
        })


    }
    window.onload=init;
    function modify() {
            $.ajax({
                type: "POST"
                ,dataType:"json"
                    ,url:"${pageContext.request.contextPath}/jsp/root.do?method=labModify&labName="+$('#input_labName').val()+"&labTotal="+$('#input_labCapa').val()+"&labTag="+$('#select_labType option:selected').val()+"&labIntroduce="+$('#input_labRes').val()
                ,success:function (data) {
                    if (data.ok === "yes"){
                        layer.msg('修改成功',{time:1000},function () {
                            parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                    }else if (data.ok === "no"){
                        layer.msg('修改失败');
                    }
                }
                ,error:function () {
                    layer.msg('由于异常原因，修改失败');
                }
            })
        }

</script>
</body>
</html>